package cn.lili.controller.system;

import cn.hutool.core.collection.CollectionUtil;
import cn.lili.cache.util.RedisUtil;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.system.constants.DictConstant;
import cn.lili.modules.system.entity.dos.Dictionary;
import cn.lili.modules.system.entity.params.DictionarySearchParams;
import cn.lili.modules.system.service.DictionaryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 中奖记录管理
 */
@RestController
@AllArgsConstructor
@RequestMapping("/system/dictionary")
public class DictionaryController {

    private DictionaryService dictionaryService;

    private RedisUtil redisUtil;

    /**
     * 获取字典码
     * @param code code
     *
     * @return 字典集合
     */
    @GetMapping("/getListByCode")
    public ResultMessage<Object> getListByCode(@RequestParam String code, Integer value) {
        return ResultUtil.data(dictionaryService.dictionryListByCode(code, value));
    }

    /**
     * 分页查询
     * @param dictionarySearchParams 查询条件
     *
     * @return 分页结果
     */
    @GetMapping("/page")
    public ResultMessage<Object> pageVo(DictionarySearchParams dictionarySearchParams) {
        return ResultUtil.data(dictionaryService.pageVo(dictionarySearchParams));
    }

    /**
     * 添加字典码
     * @return 是否成功
     */
    @PostMapping("/add")
    public ResultMessage<Object> add(@RequestBody Dictionary dictionary) {
        dictionaryService.check(dictionary);
        dictionary.setStatus(DictConstant.STATUS_1);
        return ResultUtil.data(dictionaryService.save(dictionary));
    }

    /**
     * 删除字典
     * @param ids 删除id
     * @return 是否成功
     */
    @DeleteMapping("/delete")
    public ResultMessage<Object> delete(@RequestParam String ids) {
        List<Dictionary> list = dictionaryService.list(new LambdaQueryWrapper<Dictionary>().in(Dictionary::getId, (Object[]) ids.split(",")));
        if (CollectionUtil.isEmpty(list)) {
            return ResultUtil.success();
        }
        for (Dictionary dictionary : list) {
            redisUtil.removeKey(dictionary.getCode());
        }
        return ResultUtil.data(dictionaryService.remove(new LambdaQueryWrapper<Dictionary>().in(Dictionary::getId, list.stream().map(Dictionary::getId).collect(Collectors.toList()))));
    }

    /**
     * 修改字典
     */
    @PutMapping("/update")
    public ResultMessage<Object> update(@RequestBody Dictionary dictionary) {
        dictionaryService.check(dictionary);
        return ResultUtil.data(dictionaryService.updateById(dictionary));
    }

    /**
     * 状态变动
     */
    @PutMapping("/change")
    public ResultMessage<Object> change(@RequestParam String id, @RequestParam Integer status) {
        return ResultUtil.data(dictionaryService.change(id, status));
    }

    @GetMapping("/getActivityPop")
    public ResultMessage<Boolean> getActivityPop () {
        return ResultUtil.data(dictionaryService.getActivityPop());
    }
}
